<template>
  <div>
    <el-dialog
      :title="obj.isadd ? '角色添加' : '角色编辑'"
      :visible.sync="obj.istrue"
      width="50%"
      :before-close="handleClose"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="角色名称">
          <el-input v-model="form.rolename"></el-input>
        </el-form-item>

        <el-tree
          :data="list"
          show-checkbox
          node-key="id"
          ref="tree"
          :props="{
            children: 'children',
            label: 'title',
          }"
        >
        </el-tree>

        <el-form-item label="状态">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="quxiao()">取 消</el-button>
        <el-button type="primary" @click="add" v-if="obj.isadd"
          >添 加</el-button
        >
        <el-button type="primary" v-else @click="bianji">编 辑</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapActions, mapGetters } from "vuex";
import { roleadd, roleinfo, roleedit } from "../../../request/api";
import { successAlert, warningAlert } from "../../../util/alert";
export default {
  props: ["obj"],
  data() {
    return {
      dialogVisible: true,
      form: {
        rolename: "",
        menus: "",
        status: "",
      },
    };
  },
  methods: {
    ...mapActions({
      reqchangelist: "menu/reqchangelist",
      reqchangerolelist: "role/reqchangelist",
    }),
    // 点击x执行的函数
    handleClose() {
      this.quxiao();
    },
    // 清空数据
    empty() {
      this.form = {
        rolename: "",
        menus: "",
        status: "",
      };
    },
    // 点击取消
    quxiao() {
      this.obj.istrue = false;
      this.empty();
      this.$refs.tree.setCheckedKeys([]);
    },
    // 点击添加
    add() {
      // console.log(this.$refs.tree.getCheckedKeys());
      this.form.menus = JSON.stringify(this.$refs.tree.getCheckedKeys());
      // console.log(this.form);
      roleadd(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          // 关闭弹窗并清空
          this.quxiao();
          // 刷新列表
          this.reqchangerolelist();
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
    // 获取一条数据
    getinfo(id) {
      roleinfo({ id: id }).then((res) => {
        // console.log(res);
        this.form = {
          ...res.data.list,
          // this.$refs.tree.setCheckedKeys([3])
          menus: JSON.parse(res.data.list.menus),
        };
        this.$refs.tree.setCheckedKeys(this.form.menus);
        this.form.id = id;
      });
    },
    // 点击编辑
    bianji() {
      this.form.menus = JSON.stringify(this.$refs.tree.getCheckedKeys());
      // 修改数据
      roleedit(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          // 关闭弹窗并清空
          this.quxiao();
          // 刷新列表
          this.reqchangerolelist();
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
  },
  computed: {
    ...mapGetters({
      list: "menu/list",
    }),
  },
  mounted() {
    this.reqchangelist();
  },
};
</script>

<style lang="less" scoped>
</style>